package com.gexiaobao.pigeon.viewmodel.request

import androidx.lifecycle.MutableLiveData
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.model.bean.AccountUserInfo
import com.gexiaobao.pigeon.app.model.bean.LoftPayInfoResponse
import com.gexiaobao.pigeon.app.model.bean.MemberCodeResponse
import com.gexiaobao.pigeon.app.model.bean.MessageListResponse
import com.gexiaobao.pigeon.app.model.bean.UiState
import com.gexiaobao.pigeon.app.model.bean.UserAuthsInfoResponse
import com.gexiaobao.pigeon.app.model.bean.UserInfoResponse
import com.gexiaobao.pigeon.app.model.param.LoftPayInfoParam
import com.gexiaobao.pigeon.app.network.ListDataUiState
import com.gexiaobao.pigeon.app.network.apiService
import com.gexiaobao.pigeon.app.util.RxToast
import me.hgj.jetpackmvvm.base.appContext
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.request
import me.hgj.jetpackmvvm.state.ResultState

/**
 * @Author      : hxw
 * @Date        : 2023/2/13 16:19
 * @Describe    :
 */
class RequestHomeViewModel : BaseViewModel() {

    private var pageNo: Int = 1
    private var pageSize: Int = 3

    /** 用户数据 */
    val userInfoResult: MutableLiveData<UserInfoResponse> = MutableLiveData()

    /** 删除消息 */
    val deleteMsgResult: MutableLiveData<UiState> = MutableLiveData()

    /** 获取棚号 */
    var memberNumResult: MutableLiveData<MemberCodeResponse> = MutableLiveData()
    fun getMemberNum(id: String) {
        request({ apiService.getMemberNum(id) }, {
            memberNumResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /**  消息列表  */
    var messageList: MutableLiveData<ListDataUiState<MessageListResponse.DataList>> = MutableLiveData()
    fun getMessageList(isRefresh: Boolean) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.getMessageList(pageNo, pageSize, "2", "", "", "") }, {
            pageNo++
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                hasMore = it.hasMore(),
                listData = it.data
            )
            messageList.value = listDataUiState
        }, {
            val listDataUiState =
                ListDataUiState(isSuccess = false, isRefresh = isRefresh, errMessage = it.msg, listData = arrayListOf<MessageListResponse.DataList>())
            messageList.value = listDataUiState
        })
    }

    /** 管理员获取公棚权限列表 */
    val userAuthsResult: MutableLiveData<UserAuthsInfoResponse> = MutableLiveData()
    fun getUserAuths() {
        request({ apiService.getUserAuths() }, {
            //请求成功
            userAuthsResult.value = it
        }, {
            RxToast.error(it.msg)
        })
    }

    /** 获取单个用户信息 */
    fun getUserDetail() {
        request({ apiService.getUserDetail() }, {
            //请求成功
            userInfoResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 删除消息 */
    fun deleteMessage(id: String) {
        request({ apiService.deleteMessage(id) }, {
            //请求成功
            val result = UiState(isSuccess = true)
            deleteMsgResult.value = result
        }, {
            val result = UiState(isSuccess = true, errorMsg = it.msg)
            deleteMsgResult.value = result
        })
    }

    /** 查询绑定的用户信息 */
    val accountUserInfoResult = MutableLiveData<ResultState<AccountUserInfo>>()
    fun getUserMemberInfo() {
        request(
            { apiService.getUserMemberInfo() },
            accountUserInfoResult,
            true,
            appContext.getString(R.string.loading)
        )
    }

    /** 生成公棚赛鸽支付订单 */
    var loftPayInfoResult: MutableLiveData<LoftPayInfoResponse> = MutableLiveData()
    fun genLoftPayInfo(param: LoftPayInfoParam) {
        request({ apiService.genLoftPayInfo(param) }, {
            loftPayInfoResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }
}